<template>
  <div id="screenfull" v-if="routeScreenFull">
    <svg-icon
      :name="ifFullScreen ? 'exit-fullscreen' : 'fullscreen'"
      @click="click"
    />
  </div>
</template>

<script lang="ts">
import screenfull from 'screenfull'
import { Component, Vue } from 'vue-property-decorator'
import { SettingsModule } from '@/store/modules/settings'
const sf = screenfull

@Component({
  name: 'Screenfull'
})
export default class extends Vue {
  private value2 = true
  get ifFullScreen() {
    return SettingsModule.isFullScreen
  }
  get routeScreenFull() {
    return this.$route.path.includes('dashboard')
  }
  mounted() {
    if (sf && sf.isEnabled) {
      sf.on('change', this.change)
    }
  }

  beforeDestroy() {
    if (sf && sf.isEnabled) {
      sf.off('change', this.change)
    }
  }

  private change() {
    if (sf && sf.isEnabled) {
      SettingsModule.ChangeSetting({
        key: 'isFullScreen',
        value: sf.isFullscreen
      })
    }
  }

  private click() {
    if (sf) {
      if (!sf.isEnabled) {
        this.$message({
          message: 'you browser can not work',
          type: 'warning'
        })
        return false
      }
      sf.toggle()
    }
  }
}
</script>
